CC		= g++
MPICC	= mpic++

SRCEXTS	= .cpp
HDREXTS	= .h
OBJEXT	= .o

CFLAGS	= -O2 -Wall -Iinclude -fopenmp `$(MPICC) -showme:compile`
LDFLAGS	= -fopenmp -lm -lrt `$(MPICC) -showme:link`

SRCDIR	= src
INCDIR	= include
OBJDIR	= bin

PROGRAM = jacobi

SOURCES = $(foreach d,$(SRCDIR),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
HEADERS = $(foreach d,$(INCDIR),$(wildcard $(addprefix $(d)/*,$(HDREXTS))))
OBJS    = $(addsuffix .o, $(addprefix $(OBJDIR)/, $(basename $(SOURCES))))
MOBJS	= $(addsuffix .o, $(addprefix $(OBJDIR)/, $(basename $(MAINS))))

.PHONY: all objs clean clean_all show exec
.SUFFIXES:

all: $(PROGRAM)

objs: $(OBJS)

$(OBJDIR)/%$(OBJEXT):%$(SRCEXTS) $(HEADERS)
	mkdir $(dir $@) -p
	$(CC) -c $< -o $@ $(CFLAGS)
	
$(PROGRAM):$(OBJS)
	$(CC) $(OBJS) -o $(OBJDIR)/$@ $(LDFLAGS)
	@echo Type \'./$(OBJDIR)/$@\' to execute.
	
clean:
	find bin -iname *$(OBJEXT) -delete
	
clean_all:
	rm -R $(OBJDIR)

show:
	@echo 'SRCDIR      :' $(SRCDIR)
	@echo 'INCDIR      :' $(INCDIR)
	@echo 'MDIR        :' $(MDIR)
	@echo 'OBJDIR      :' $(OBJDIR)
	@echo 'SOURCES     :' $(SOURCES)
	@echo 'MAINS       :' $(MAINS)
	@echo 'HEADERS     :' $(HEADERS)
	@echo 'OBJS        :' $(OBJS)
	@echo 'MOBJS       :' $(MOBJS)
	@echo 'CFLAGS      :' $(CFLAGS)
	@echo 'LDFLAGS     :' $(LDFLAGS)
	
